/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds;

import net.worlds.object.Box;
import net.worlds.object.Model;
import net.worlds.object.Part;
import net.worlds.object.Tool;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.dynamics.ContactFilter;


public class XContactFilter implements ContactFilter{

    public boolean shouldCollide(Shape shape1, Shape shape2) {
        Part p1 = shape1.getUserData();
        Part p2 = shape2.getUserData();
        /*if(shape1.getBody().m_isExplosion && shape2.getBody().isStatic())
            return false;
        else if(shape2.getBody().m_isExplosion && shape1.getBody().isStatic())
            return false;
        if(shape1.getBody().m_isExplosion && shape2.getBody().m_isExplosion)
            return false;
        if(shape1.getBody().m_isExplosion && shape2.getUserData() != null && shape2.getUserData().isCharacter)
            return false;
        else if(shape2.getBody().m_isExplosion && shape1.getUserData() != null && shape1.getUserData().isCharacter)
            return false;*/
        if(shape1.isRope || shape2.isRope)
            return false;
        if(p1 == null || p2 == null)
            return true;
        try {
            if((p1.inUse || p2.inUse) && Main.isServer())
                return false;
            else if(p1.disabled || p2.disabled)
                return false;
            else if(p1.isCharacter && !p2.characterCollide)
                return false;
            else if(p2.isCharacter && !p1.characterCollide)
                return false;
            else if(p1.isCharacter && p2.isRemoteCharacter)
                return false;
            else if(p2.isCharacter && p1.isRemoteCharacter)
                return false;
            else if(p1.isRemoteCharacter && p2.isRemoteCharacter)
                return false;
            else if(p1.isCharacter && p1.getVelocity().y > 0.001 && p2 instanceof Box && p2.getBooleanProp("Platform"))
                return false;
            else if(p2.isCharacter && p2.getVelocity().y > 0.001 && p1 instanceof Box && p1.getBooleanProp("Platform"))
                return false;
            else if(Main.isServer() && p1.getParent() instanceof net.worlds.object.Character && p2.getParent() instanceof net.worlds.object.Character)
                return false;
            if (p1.getBooleanProp("Collidable") == false || p2.getBooleanProp("Collidable") == false || p1.noCollide || p2.noCollide)
                return false;
            if(p1.getParent() == p2.getParent() && p1.getParent() instanceof Model && !((Model)p1.getParent()).collideChildren)
                return false;
            if(p1.getIntProp("Layer") != p2.getIntProp("Layer"))
                return false;
        } catch (WorldsException ex) {
            ex.printStackTrace();
        }
        return true;

    }

    public boolean rayCollide(Object userData, Shape shape) {
        return true;
    }

}
